package com.hd.util;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

/**
 * 
 * Cookie 工具类
 *
 */
public final class SessionUtils {
	
	protected static final Logger logger = Logger.getLogger(SessionUtils.class);
	
	private static final String SESSION_USER = "session_user";

	private static final String SESSION_VALIDATECODE = "session_validatecode";//验证码
	
	
	private static final String SESSION_ACCESS_URLS = "session_access_urls"; //系统能够访问的URL
	
	
	private static final String SESSION_MENUBTN_MAP = "session_menubtn_map"; //系统菜单按钮

	private static final String SESSION_MENU_MAP = "session_menu_map";
	
	private static final HashMap<String,Object> SESSION_MENU_MAP_BYID = new HashMap<String,Object>();
	private static final String SESSION_MENU_MAP_BYIDNAME = new String();

	public static final String ISLOADED = "session_is_load";

	/**
	  * 设置session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static void setAttr(HttpServletRequest request,String key,Object value){
		 request.getSession(true).setAttribute(key, value);
	 }
	 
	 
	 /**
	  * 获取session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static Object getAttr(HttpServletRequest request,String key){
		 return request.getSession(true).getAttribute(key);
	 }
	 
	 /**
	  * 删除Session值
	  * @param request
	  * @param key
	  */
	 public static void removeAttr(HttpServletRequest request,String key){
		 request.getSession(true).removeAttribute(key);
	 }
	 
//	 /**
//	  * 设置用户信息 到session
//	  * @param request
//	  * @param user
//	  */
//	 public static void setUser(HttpServletRequest request,HdUser user){
//		 request.getSession(true).setAttribute(SESSION_USER, user);
//	 }
//	 
//	 
//	 /**
//	  * 从session中获取用户信息
//	  * @param request
//	  * @return SysUser
//	  */
//	 public static HdUser getUser(HttpServletRequest request){
//		return (HdUser)request.getSession(true).getAttribute(SESSION_USER);
//	 }
//	 
	 
	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static void removeUser(HttpServletRequest request){
		removeAttr(request, SESSION_USER);
	 }
	 
	 
	 /**
	  * 设置验证码 到session
	  * @param request
	  * @param user
	  */
	 public static void setValidateCode(HttpServletRequest request,String validateCode){
		 request.getSession(true).setAttribute(SESSION_VALIDATECODE, validateCode);
	 }
	 
	 
	 /**
	  * 从session中获取验证码
	  * @param request
	  * @return SysUser
	  */
	 public static String getValidateCode(HttpServletRequest request){
		return (String)request.getSession(true).getAttribute(SESSION_VALIDATECODE);
	 }
	 
	 
	 /**
	  * 从session中获删除验证码
	  * @param request
	  * @return SysUser
	  */
	 public static void removeValidateCode(HttpServletRequest request){
		removeAttr(request, SESSION_VALIDATECODE);
	 }
	 
	 
	 
	 
	 /**
	  * 判断当前登录用户是否超级管理员
	  * @param request
	  * @return
	  */
	 public static void setAccessUrl(HttpServletRequest request,List<String> accessUrls){ //判断登录用户是否超级管理员
		 setAttr(request, SESSION_ACCESS_URLS, accessUrls);
	 }
	 
	 
	 
	 /**
	  * 判断URL是否可访问
	  * @param request
	  * @return
	  */
	 public static boolean isAccessUrl(HttpServletRequest request,String url){ 
		 List<String> accessUrls = (List)getAttr(request, SESSION_ACCESS_URLS);
		 if(accessUrls == null ||accessUrls.isEmpty() || !accessUrls.contains(url)){
			 return false;
		 }
		 return true;
	 }
	 
//	public static void setMemuBtnMapByID(HttpServletRequest request,
//			List<Sysmeun> meunList, String meunid) {
//		// TODO 自动生成的方法存根
//		SESSION_MENU_MAP_BYID.put(meunid, meunList);
//		setAttr(request, SESSION_MENU_MAP_BYIDNAME, SESSION_MENU_MAP_BYID);
//	}
//	
//	public static List<Sysmeun> getMemuBtnMapByID(HttpServletRequest request,String meunid) {
//		return (List<Sysmeun> )SESSION_MENU_MAP_BYID.get(meunid);
//	}
//	 /**
//	  * 设置菜单按钮
//	  * @param request
//	  * @param btnMap
//	  */
//	 public static void setMemuBtnMap(HttpServletRequest request,List<Sysmeun> ilistst){ //判断登录用户是否超级管理员
//		 setAttr(request, SESSION_MENUBTN_MAP, ilistst);
//	 }
//	 
//	 /**
//	  * 获取菜单按钮
//	  * @param request
//	  * @param btnMap
//	  */
//	 public static List<Sysmeun> getMemuBtnMap(HttpServletRequest request){ //判断登录用户是否超级管理员
//		 List<Sysmeun> btnMap  = (List<Sysmeun>)getAttr(request, SESSION_MENUBTN_MAP);
//		 return btnMap;
//	 }
//
//
//	public static void setMemuMap(HttpServletRequest request,
//			Map<String, List<Sysmeun>> r) {
//		setAttr(request, SESSION_MENU_MAP, r);
//		
//	}
//
//
//	public static Map<String, List<Sysmeun>> getMemuMap(HttpServletRequest request) {
//		 Map<String, List<Sysmeun>> btnMap  = (Map<String, List<Sysmeun>>)getAttr(request, SESSION_MENU_MAP);
//		 return btnMap;
//	}

	 
//		private static final String SESSION_ACCESS_URLS = "session_access_urls"; //系统能够访问的URL
//		
//		
//		private static final String SESSION_MENUBTN_MAP = "session_menubtn_map"; //系统菜单按钮
	
}